<?php session_start(); ?>
<?php
if (isset($_SESSION['lang']))
    require_once "./language_files/co-authors_" . $_SESSION['lang'] . ".php";
else
    require_once "./language_files/co-authors_en.php";
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link href="style3.css" rel="stylesheet" type="text/css" />
        <link href="jquery-ui-1.8.21.custom.css" rel="stylesheet" type="text/css" />
        <?php include 'jquery.php';
        require_once "functions.php";
        ?>
        <script type="text/javascript" src="functions.js"></script>
        <script type="text/javascript" src="sortable.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                check_top();
                $(document).on( "click", ".save", function() {
                    email = $("#email").val();
                    Fname = $("#Fname").val();
                    Lname = $("#Lname").val();
                    confID = $("#confID").val();
                    paper = $("#paperID").val();
                    $.ajax ({
                        data:{email:email,Fname:Fname,Lname:Lname,paper:paper,confID:confID},
                        type: 'POST',
                        url:  'add_coauthor.php',
                        success: function(response) {
                            if(response == 1) {
                                window.location.href=window.location.href;
                            } else {
                                alert(response);
                            }
                        }
                    });
                });
                //make table rows sortable
                $('.myTable').sortable({
                    start: function (event, ui) {
                        //fix firefox position issue when dragging.
                        if (navigator.userAgent.toLowerCase().match(/firefox/) && ui.helper !== undefined) {
                            ui.helper.css('position', 'absolute').css('margin-top', $(window).scrollTop());
                            //wire up event that changes the margin whenever the window scrolls.
                            $(window).bind('scroll.sortableplaylist', function () {
                                ui.helper.css('position', 'absolute').css('margin-top', $(window).scrollTop());
                            });
                        }
                    },
                    beforeStop: function (event, ui) {
                        //undo the firefox fix.
                        if (navigator.userAgent.toLowerCase().match(/firefox/) && ui.offset !== undefined) {
                            $(window).unbind('scroll.sortableplaylist');
                            ui.helper.css('margin-top', 0);
                        }
                    },
                    helper: function (e, ui) {
                        ui.children().each(function () {
                            $(this).width($(this).width());
                        });
                        return ui;
                    },
                    scroll: true,
                    update: function(i) {
                        $.ajax({
                            type: "POST",
                            url: "sort_coauthors.php",
                            data: $(this).sortable('serialize'),
                            success: function(response) {
                                if(response == 1) {
                                    window.location.href=window.location.href;
                                } else {
                                    alert(response);
                                }
                            }
                        });
                    }
                }).disableSelection();
                $("#add").click(function() {
                    resize_iframe();
                    $(".no_border").append("<br /><label>Co-author email:</label><input type='text' name='author_email' id='email'/><label>Co-author first name:</label><input id='Fname' type='text' name='Fname'/><label>Co-author last name:</label><input id='Lname' type='text' name='Lname'/><input type='button' id='save' class='save' name='save' value='Add'/>");
                });
                $(document).on("click", "#remove", function(){
                    var conf = confirm("Are you sure you want to remove co-author?");
                    if(conf == true) {
                        email = $(this).attr("email");
                        paper = $(this).attr("paper");
                        $.ajax ({
                            data:{email:email,paper:paper},
                            type: 'POST',
                            url:  'delete_coauthor.php',
                            success: function(response) {
                                location.reload();
                            }
                        });
                    }
                });

            });
        </script>
        <title><?php echo $lang['title']; ?></title>
    </head>
    <body>
        <?php
        if (isset($_GET['conference_id'])) {

            $conference_id = $_GET['conference_id'];
            $confname = get_conference_name($conference_id);
            echo"</br><span class=breadcrumbs><a href='subscriberConferencePage.php?conference_id={$conference_id}'>" . $confname . "</a>" . " > " . $lang['manage'] . "</span> </br></br>";
        }
        ?>
        <h2><?php echo $lang['invite']; ?></h2>
        <?php
        $member_id = $_SESSION['member_id'];
        //$member_email = $_SESSION['email'];
        echo "<h3>" . $lang['current'] . "</h3>";
        if (isset($_GET['paper_id']) && isset($_GET['conference_id'])) {
            $paper_id = $_GET['paper_id'];
            $conference_id = $_GET['conference_id'];
            ?> <input type="hidden" value="<?php echo $conference_id ?>" id="confID" name="confID"/>
            <input type="hidden" value="<?php echo $paper_id ?>" id="paperID" name="paperID"/>
            <?
            $main_author = mysql_query("SELECT author_id FROM paper WHERE
               paper_id = $paper_id AND author_id = $member_id AND conference_id = $conference_id") or die(mysql_error());
            if (mysql_num_rows($main_author) == 1) {
                $co_authors = mysql_query("SELECT m.id, m.sort, m.author_email, m.paper_id, b.first_name,b.last_name FROM co_authors m
                 INNER JOIN member b ON (b.email = m.author_email)
                        WHERE
                 paper_id=$paper_id ORDER BY sort ASC") or die(mysql_error());
                if (mysql_num_rows($co_authors) == 0) {
                    echo "<p align='center'>" . $lang['no_co-author'] . "</p>";
                } else {
                    echo "<table width=100%>";
                    echo "<tr><td style='background-color:#003366; color:white; text-align:center;'>" . $lang['author'] . "</td>
                <td style='background-color:#003366; color:white; text-align:center;'>" . $lang['Affiliation'] . "</td>
                <td style='background-color:#003366; color:white; text-align:center;'>" . $lang['Remove'] . "</td>
                 </tr>";
                    echo "<tbody title='drag & drop to sort' class='myTable' type='none'>";
                    while ($row = mysql_fetch_assoc($co_authors)) {
                        $author_email = formatText_safe($row['author_email']);
                        $author_Fname = formatText_safe($row['first_name']);
                         $author_Lname = formatText_safe($row['last_name']);
                        echo "<tr id='menu_" . $row['id'] . "'><td>{$author_email}
                         <td>{$author_Fname} {$author_Lname}</td>";
                        ?><td><button class='remove' id='remove' name='remove' email="<?php echo $row['author_email'] ?>"
                                paper="<?php echo $row['paper_id'] ?>"><?php echo $lang['Remove']; ?></button></td></tr>
                    <?
                }
                echo "</tbody>";
                echo "</table>";
            }
            ?><table><tr><td class="no_border"><button class="add" name="add"></button>
                    <label id="add" class="cursor_pointer" onclick=""> <?php echo $lang['add_authors']; ?></label></td></tr></table><?
        } else {
            $co_authors2 = mysql_query("SELECT c.sort, c.author_email, m.first_name, m.last_name FROM co_authors c
                    INNER JOIN member m ON (m.email = c.author_email)
                    WHERE
                 paper_id=$paper_id ORDER BY sort ASC") or die(mysql_error());
            echo "<table width=100%>";
            echo "<tr><td style='background-color:#003366; color:white; text-align:center;'>" . $lang['author'] . "</td>
                <td style='background-color:#003366; color:white; text-align:center;'>" . $lang['Affiliation'] . "</td></tr>";
            if (mysql_num_rows($co_authors2) == 0) {
                echo "<tr><td colspan='4' style='text-align:center;'>" . $lang['no_co-author'] . "</td></tr>";
            } else {
                while ($row = mysql_fetch_assoc($co_authors2)) {
                    $author_email = formatText_safe($row['author_email']);
                    $author_Fname = formatText_safe($row['first_name']);
                    $author_Lname = formatText_safe($row['last_name']);
                    echo "<tr>
                         <td>{$author_email}</td>
                         <td>{$author_Fname} {$author_Lname}</td></tr>";
                }
            }
        }

        echo "</table>";
        mysql_close();
    }
    ?>
</body>
</html>
